<?php

namespace api\common\models;

use Yii;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property int    $user_id
 * @property string $nickname 用户名
 * @property string $headimg 头像
 * @property string $sex 1男 2女 0未知
 * @property string $phone 手机号
 * @property string $openid
 * @property string $account_type 身份类型 1普通用户 2认证车主 3认证商家
 * @property string $account_state 账号状态 1可用 0禁止登陆 -1已注销
 * @property string $self_introduce 自我描述
 * @property int    $register_time 注册时间
 * @property string $access_token
 */
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
    /**
     * {@inheritdoc}
     */
    public static function tableName ()
    {
        return '{{%user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules ()
    {
        return [
            [ [ 'sex' , 'account_type' , 'account_state' ] , 'string' ] ,
            [ [ 'register_time' ] , 'integer' ] ,
            [ [ 'nickname' ] , 'string' , 'max' => 15 ] ,
            [ [ 'headimg' ] , 'string' , 'max' => 150 ] ,
            [ [ 'phone' ] , 'string' , 'max' => 11 ] ,
            [ [ 'openid' ] , 'string' , 'max' => 30 ] ,
            [ [ 'self_introduce' ] , 'string' , 'max' => 100 ] ,
            [ [ 'access_token' ] , 'string' , 'max' => 32 ] ,
            [ [ 'phone' ] , 'unique' ] ,
            [ [ 'openid' ] , 'unique' ] ,
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels ()
    {
        return [
            'user_id'        => 'User ID' ,
            'nickname'       => 'Nickname' ,
            'headimg'        => 'Headimg' ,
            'sex'            => 'Sex' ,
            'phone'          => 'Phone' ,
            'openid'         => 'Openid' ,
            'account_type'   => 'Account Type' ,
            'account_state'  => 'Account State' ,
            'self_introduce' => 'Self Introduce' ,
            'register_time'  => 'Register Time' ,
            'access_token'   => 'Access Token' ,
        ];
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity ( $id )
    {
        return static::findOne( $id );
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken ( $token , $type = NULL )
    {
        //throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
        return static::findOne( [ 'access_token' => $token ] );
    }

    /**
     * @inheritdoc
     */
    public function getId ()
    {
        return $this->getPrimaryKey();
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey () { }

    /**
     * @inheritdoc
     */
    public function validateAuthKey ( $authKey ) { }
}
